package com.hera.base.web.vaadin.mantenimiento;

import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;

import com.hera.base.util.TableCustom;
import com.hera.base.web.AbstractConfirmDialog;
import com.hera.base.web.BaseUI;
import com.hera.base.web.MantenimientoMaestroDetalle;
import com.hera.process.config.rest.delegate.FormDelegate;
import com.hera.process.form.domain.Dynaform;
import com.hera.process.form.domain.FormField;
import com.vaadin.annotations.AutoGenerated;
import com.vaadin.data.Property.ValueChangeEvent;
import com.vaadin.data.Property.ValueChangeListener;
import com.vaadin.event.FieldEvents.TextChangeEvent;
import com.vaadin.event.FieldEvents.TextChangeListener;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Button.ClickListener;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.VerticalLayout;
import com.vaadin.ui.Window;

public class ProcessFieldUI extends BaseUI implements MantenimientoMaestroDetalle,ClickListener {

	/*- VaadinEditorProperties={"grid":"RegularGrid,20","showGrid":true,"snapToGrid":true,"snapToObject":true,"movingGuides":false,"snappingDistance":10} */

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	@AutoGenerated
	private VerticalLayout mainLayout;
	@AutoGenerated
	private VerticalLayout verticalLayout_1;
	@AutoGenerated
	private HorizontalLayout lytMaestro;
	@AutoGenerated
	private VerticalLayout verticalLayout_3;
	@AutoGenerated
	private HorizontalLayout horizontalLayout_5;
	@AutoGenerated
	private Button btnCancelar;
	@AutoGenerated
	private Button btnAceptar;
	@AutoGenerated
	private TableCustom tblfMaestro;
	
	AbstractConfirmDialog iConfirmDialog;
	/**
	 * The constructor should first build the main layout, set the
	 * composition root and then do any custom initialization.
	 *
	 * The constructor will not be automatically regenerated by the
	 * visual editor.
	 */
	public ProcessFieldUI(Dynaform dynaform, AbstractConfirmDialog iConfirmDialog) {
		buildMainLayout();
		setCompositionRoot(mainLayout);
		this.dynaform = dynaform;
		this.iConfirmDialog = iConfirmDialog;
	}
	
	
	private static final Logger log = Logger.getLogger(ProcessFieldUI.class);
	
	@Autowired
	FormDelegate formDelegate;
	FormField formField;
	Dynaform dynaform;
	
	@Override
	public void buttonClick(ClickEvent event) {
		if(event.getButton().equals(btnAceptar)){
			iConfirmDialog.aceptar(btnAceptar,(Window)this.getParent().getParent(), formField);
		}else if(event.getButton().equals(btnCancelar)){
			iConfirmDialog.cancelar(btnCancelar,(Window)this.getParent().getParent());
		}
		
	}

	@Override
	public void cargarFormulario() {
				
		//tblfMaestro.setTitulo(" Lista Formularios del Stage " + stage.getName());
		tblfMaestro.setHeight("150px");
		
		tblfMaestro.addColumn("name", "Nombre", 100, true, String.class);
		tblfMaestro.addColumn("type.name", "Tipo", 100, true, String.class);
		//tblfMaestro.addColumn("schema", "Modo", 250, true, String.class);
		
		tblfMaestro.construir();
		tblfMaestro.addTextChangeListener(new TextChangeListener() {
			private static final long serialVersionUID = 1L;

			@Override
			public void textChange(TextChangeEvent event) {
				buscarMaestro(event);
				
			}
		});
		
		tblfMaestro.addValueChangeListener(new ValueChangeListener() {
			private static final long serialVersionUID = 1L;

			@Override
			public void valueChange(ValueChangeEvent event) {
				formField = (FormField)tblfMaestro.getSelectedItem(event);				
			}
		});
		
		btnAceptar.addClickListener(this);
		btnCancelar.addClickListener(this);
	}

	@Override
	public void buscarMaestro() {
		buscarMaestro(null);		
	}

	@Override
	public void buscarMaestro(TextChangeEvent event) {
		log.info("inicio Buscar FormField");
		List<FormField> lstFormField = formDelegate.findFormFieldNoAsignado(dynaform);
		tblfMaestro.setLstData(lstFormField);		
	}

	@Override
	public void limpiarMaestro() {
		log.info("inicio limpiarMaestro");			
		log.info("fin limpiarMaestro");
	}

	@Override
	public void eliminarMaestro() {		
		
	}

	@Override
	public void guardarMaestro() {
		
		
	}

	@Override
	public void buscarDetalle() {
		
		
	}

	@Override
	public void buscarDetalle(TextChangeEvent event) {
		
		
	}

	@Override
	public void limpiarDetalle() {
		
		
	}

	@Override
	public void eliminarDetalle() {
		
		
	}

	@Override
	public void guardarDetalle() {
		
		
	}

	@Override
	protected void postConstruct() {
		cargarFormulario();
		buscarMaestro();
		
	}
	
	public Button getBtnAceptar(){
		return btnAceptar;
	}
	
	public Button getBtnCancelar(){
		return btnCancelar;
	}


@AutoGenerated
private VerticalLayout buildMainLayout() {
	// common part: create layout
	mainLayout = new VerticalLayout();
	mainLayout.setImmediate(false);
	mainLayout.setWidth("-1px");
	mainLayout.setHeight("-1px");
	mainLayout.setMargin(false);
	
	// top-level component properties
	setWidth("-1px");
	setHeight("-1px");
	
	// verticalLayout_1
	verticalLayout_1 = buildVerticalLayout_1();
	mainLayout.addComponent(verticalLayout_1);
	
	return mainLayout;
}

@AutoGenerated
private VerticalLayout buildVerticalLayout_1() {
	// common part: create layout
	verticalLayout_1 = new VerticalLayout();
	verticalLayout_1.setImmediate(false);
	verticalLayout_1.setWidth("-1px");
	verticalLayout_1.setHeight("-1px");
	verticalLayout_1.setMargin(false);
	verticalLayout_1.setSpacing(true);
	
	// lytMaestro
	lytMaestro = buildLytMaestro();
	verticalLayout_1.addComponent(lytMaestro);
	
	return verticalLayout_1;
}

@AutoGenerated
private HorizontalLayout buildLytMaestro() {
	// common part: create layout
	lytMaestro = new HorizontalLayout();
	lytMaestro.setImmediate(false);
	lytMaestro.setWidth("-1px");
	lytMaestro.setHeight("-1px");
	lytMaestro.setMargin(false);
	lytMaestro.setSpacing(true);
	
	// verticalLayout_3
	verticalLayout_3 = buildVerticalLayout_3();
	lytMaestro.addComponent(verticalLayout_3);
	
	return lytMaestro;
}

@AutoGenerated
private VerticalLayout buildVerticalLayout_3() {
	// common part: create layout
	verticalLayout_3 = new VerticalLayout();
	verticalLayout_3.setImmediate(false);
	verticalLayout_3.setWidth("270px");
	verticalLayout_3.setHeight("-1px");
	verticalLayout_3.setMargin(false);
	
	// tblfMaestro
	tblfMaestro = new TableCustom();
	tblfMaestro.setImmediate(false);
	tblfMaestro.setWidth("-1px");
	tblfMaestro.setHeight("-1px");
	verticalLayout_3.addComponent(tblfMaestro);
	
	// horizontalLayout_5
	horizontalLayout_5 = buildHorizontalLayout_5();
	verticalLayout_3.addComponent(horizontalLayout_5);
	
	return verticalLayout_3;
}

@AutoGenerated
private HorizontalLayout buildHorizontalLayout_5() {
	// common part: create layout
	horizontalLayout_5 = new HorizontalLayout();
	horizontalLayout_5.setImmediate(false);
	horizontalLayout_5.setWidth("-1px");
	horizontalLayout_5.setHeight("-1px");
	horizontalLayout_5.setMargin(false);
	horizontalLayout_5.setSpacing(true);
	
	// btnAceptar
	btnAceptar = new Button();
	btnAceptar.setCaption("Aceptar");
	btnAceptar.setImmediate(true);
	btnAceptar.setWidth("-1px");
	btnAceptar.setHeight("-1px");
	horizontalLayout_5.addComponent(btnAceptar);
	
	// btnCancelar
	btnCancelar = new Button();
	btnCancelar.setCaption("Cancelar");
	btnCancelar.setImmediate(true);
	btnCancelar.setWidth("-1px");
	btnCancelar.setHeight("-1px");
	horizontalLayout_5.addComponent(btnCancelar);
	
	return horizontalLayout_5;
}
	
	
}
